package real;

import javax.websocket.*;
import java.io.IOException;
import java.net.URI;
 
@ClientEndpoint
public class EchoClient {
 
    private Session session;
 
    @OnOpen
    public void onOpen(Session session) {
        System.out.println("WebSocket 连接已经建立。");
        this.session = session;
    }
 
    @OnMessage
    public void onMessage(String message, Session session) {
        System.out.println("收到服务器消息：" + message);
    }
 
    @OnClose
    public void onClose() {
        System.out.println("WebSocket 连接已经关闭。");
    }
 
    @OnError
    public void onError(Throwable t) {
        System.out.println("WebSocket 连接出现错误：" + t.getMessage());
    }
 
    public void connect(String url) throws Exception {
        WebSocketContainer container = ContainerProvider.getWebSocketContainer();
        container.connectToServer(this, new URI(url));
    }
 
    public void send(String message) throws IOException {
        session.getBasicRemote().sendText(message);
    }
 
    public void close() throws IOException {
        session.close();
    }

    public static void main(String[] args) throws Exception {
        EchoClient c = new EchoClient();
        c.connect("ws://127.0.0.1:8078");
        new Thread(()->{
            while (true){
                try {
                    c.send("{\"type\":\"1\",\"token\":\"hello ws server123123!!\"}");
                    System.out.printf("发生成功");
                    Thread.sleep(3000);
                } catch (IOException | InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }).start();
        Thread.sleep(10000000);
    }
}